package cn.edu.sgu.www.mhxysy.subject.impl;

import cn.edu.sgu.www.mhxysy.observer.Observer;
import cn.edu.sgu.www.mhxysy.subject.Subject;
import cn.edu.sgu.www.mhxysy.util.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体主题-角色高级属性主题
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Component
public class AdvancedPropertySubject implements Subject {

    private final List<Observer> observers = new ArrayList<>();

    @Override
    public void notice(Object message) {
        if (CollectionUtils.isNotEmpty(observers)) {
            for (Observer observer : observers) {
                observer.update(message);
            }
        }
    }

    @Override
    public synchronized void addObserver(Observer observer) {
        // 集合中没有该观察者才添加，防止保存多个重复的观察者
        if (!observers.contains(observer)) {
            this.observers.add(observer);
        }
    }

}